package com.ssy.controller;

import com.ssy.entity.PageBean;
import com.ssy.entity.Result;
import com.ssy.entity.Student;
import com.ssy.entity.StudentQueryParam;
import com.ssy.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询(条件查询)
     * @param studentQueryParam
     * @return
     */
    @GetMapping
    public Result Page(StudentQueryParam studentQueryParam) {
        log.info("分页查询 {}", studentQueryParam);
        PageBean pageBean = studentService.pgae(studentQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 添加学生信息
     */
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("添加学生: {}" , student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 批量删除学生信息
     */
    @DeleteMapping("{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除学生: {}" , ids);
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询学生信息(查询回显)
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("查询学生信息: {}" , id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 根据id修改学生数据
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学生信息: {}" , student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @PutMapping("violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("处理违纪: {} {}" , id, score);
        studentService.violation(id, score);
        return Result.success();
    }
}
